<?php

namespace app\common\controller;

use app\BaseController;
use app\model\RoleModel;
use app\model\UnitModel;
use app\model\UserModel;
use think\App;

class Role extends BaseController
{
    protected $mode;
    public function __construct(App $app)
    {
        parent::__construct($app);
        $this->mode = new RoleModel();
    }

    /**
     * 默认权限配置
     * @param int $mode
     * @return string
     */
    public function rulesConfig(int $mode): string
    {
        $data = [
            0 => '346,347,348,349,350,351,354,355,356,357,360,361,362,363,4,23,51,52,53,54,21,55,56,57,26,58,59,60,35,42,61,62,63,64,65,66,67,68,69,43,70,71,72,73,74,76,79,80,81,82,83,84,85,86,77,87,88,89,90,91,92,93,94,78,95,96,97,98,99,100,101,102,103,104,75,105,106,107,108,109,110,111,120,121,124,125,126,127,128,129,130,131,132,133,134,135,136,137,138,139,140,141,142,143,144,145,146,147,148,149,150,151,152,153,154,155,157,158,159,160,161,162,165,167,169,171,172,173,175,177,179,182,184,186,188,190,192,194,196,198,201,203,205,207,210,211,213,215,218,220,221,225,226,228,231,233,237,239,240,243,244,246,248,251,253,255,257,259,261,262,264,266,268,270,272,274,276,227,229,245,247,249,250,252,254,256,230,258,294,295,296,260,297,298,299,300,232,263,265,267,269,271,273,275,234,277,279,281,283,285,235,286,287,288,289,290,291,236,238,292,293,241,242,44,47,163,174,176,178,180,181,183,185,187,189,191,193,164,195,197,199,200,202,204,206,208,209,212,214,166,216,217,219,48,168,170,222,224,49,50,14,15,17,19,20,22,24,25,27,28,29,30,18,31,32,33,34,36,37,38,39,40,41,2,3,6,8,9,10,12,13,5,334,335,336,337,338,339,340,341,342,343,344,345,370,371,372,373,374,364,365,366,369',
            1 => '4,23,21,55,56,57,73,74,78,95,96,97,98,99,100,101,102,103,104,75,105,106,107,108,121,124,125,126,127,128,129,130,131,132,133,134,135,136,137,138,139,140,141,142,143,144,145,146,147,148,149,150,151,152,153,154,155,157,158,159,160,161,162,165,167,169,171,172,173,175,177,179,182,184,186,188,190,192,194,196,198,201,205,207,213,215,218,220,221,228,231,233,237,239,240,243,244,246,248,251,253,255,257,259,261,262,264,266,268,270,272,274,276,227,229,245,247,249,250,252,254,256,230,258,294,295,296,260,232,263,271,275,234,277,281,285,235,290,236,238,292,293,241,242,44,47,163,174,178,191,193,164,195,199,212,214,166,216,48,168,49,50,14,15,17,19,24,25,27,28,29,30,18,31,37,38,39,40,41,2,3,10,5,337,338,339,340,341,342,343,344,345,346,347,348,349,350,354,355,356,357,334,335,336,360,361,362,363,35,42,61,62,63,64,65,66,67,68,69,43,70,71,72,370,371,372,373,374,364,365,366,369',
            2 => '14,15,17,19,24,25,27,28,29,30,18,31,37,38,39,40,41,44,47,163,174,176,178,180,181,183,185,187,189,191,193,164,195,197,199,200,202,204,206,208,209,212,214,166,216,217,219,48,168,170,222,224,49,50,227,229,245,247,249,250,252,254,256,230,258,260,232,263,234,277,121,129,130,131,133,135,136,140,141,143,145,146,150,151,153,155,160,161,165,169,171,177,179,184,188,190,198,201,203,205,207,210,211,213,215,105,106,107,108,124,125,126,127,128,218,220,221,225,226,228,231,233',
            3 => '105,106,107,108,309,120,121,122,123,129,130,131,132,136,137,301,140,141,142,146,147,302,160,161,162,171,172,303,227,236,44,45,46,47,163,174,176,189,164,195,197,209,304,334,335,337,338,340,341,343,344,346,349,352,354,358,359,360,361,362,363,370,375,376,364,367,368'
        ];
        return $data[$mode];
    }

    /**
     * 还原角色权限
     * @return bool|string
     */
    public function restoreRulesConfig(): bool|string
    {
        $id = $this->request->param('id');
        try {
            $info = $this->mode->find($id);
            $info->rules = $this->rulesConfig($info['mode']);
            $info->save();
            return success([],'还原成功！');
        } catch (\Exception $e) {
            return error($e->getMessage(), $e->getCode(),[
                'line' => $e->getLine(),
                'file' => $e->getFile(),
            ]);
        }
    }

    public function index(): bool|string
    {
        $param = $this->request->param();
        $where = [];
        $where[] = ['unit_id','=',$this->userData['unit_id']];
        $data = $this->mode->where($where)->withCount(['user'])->paginate($param['pageSize']);
        return success($data);
    }

    public function add(): bool|string
    {
        $param = $this->request->param();
        try {
            $param['unit_id'] = $this->userData['unit_id'];
            $this->validate($param, $this->mode->addValidate());
            $param['rules'] = $this->rulesConfig($param['mode'])??'';
            $param['time'] = time();
            $this->mode->insert($param);
            return success([],'添加成功！');
        } catch (\Exception $e) {
            return error($e->getMessage(), $e->getCode(),[
                'line' => $e->getLine(),
                'file' => $e->getFile(),
            ]);
        }
    }

    public function update(): bool|string
    {
        $this->mode->startTrans();
        try {
            $param = $this->request->param();
            $this->validate($param,[
                'id|ID' => 'require|integer|max:11',
            ]);
            $info = $this->mode->find($param['id']);
            if (!$info) throw new \Exception('参数错误！');
            if (!empty($param['mode']) && $info['mode'] != $param['mode']) {
                $param['rules'] = $this->rulesConfig($param['mode']);
                $UserModel = new UserModel();
                $UserModel->where('role_id','=',$param['id'])->update(['role_mode'=>$param['mode']]);
            }
            $info->save($param);
            $this->mode->commit();
            return success([],'更新成功！');
        } catch (\Exception $e) {
            $this->mode->rollback();
            return error($e->getMessage(), $e->getCode(),[
                'line' => $e->getLine(),
                'file' => $e->getFile(),
            ]);
        }
    }

    public function del(): bool|string
    {
        $id = $this->request->param('id');
        try {
            $User = new UserModel();
            $have_user = $User->where('role_id','=',$id)->value('id');
            if ($have_user) throw new \Exception('该角色下有用户无法删除！');
            $this->mode->where('id','=',$id)->update([
                'is_del' => 1,
                'del_time' => time()
            ]);
            return success([],'删除成功！');
        } catch (\Exception $e) {
            return error($e->getMessage(), $e->getCode(),[
                'line' => $e->getLine(),
                'file' => $e->getFile(),
            ]);
        }
    }

    /**
     * 获取角色权限
     * @return bool|string
     */
    public function getRoleRule(): bool|string
    {
        $id = $this->request->param('id');
        try {
            if ($id){
                $rules = $this->mode->where('id','=',$id)->value('rules');
            }else{
                $UnitModel = new UnitModel();
                $rules = $UnitModel->where('id','=',$this->userData['unit_id'])->value('rules');
            }
            return success($rules);
        } catch (\Exception $e) {
            return error($e->getMessage(), $e->getCode(),[
                'line' => $e->getLine(),
                'file' => $e->getFile(),
            ]);
        }
    }
}
